﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBase.Form.Control;
using Microsoft.Xna.Framework;
using GameBase;

namespace GalaxyPresident
{
    class EntityInfoPanel : MyPanel
    {
        private PlanetEntity _entity;
        private MyLabel _labelInfo;
        private List<MyLabel> _listLabelDetailInfo;
        MyButton buttonDetail;

        Vector2 originalPos;
        Vector2 airPortPos;

        public delegate void ShowDetailPanelCallBack();
        private ShowDetailPanelCallBack showDetailPanelEvent;

        public ShowDetailPanelCallBack ShowDetailPanelEvent
        {
            get { return showDetailPanelEvent; }
            set { showDetailPanelEvent = value; }
        }

        public EntityInfoPanel(Vector2 position):
            base(@"Resources/Ingame/infoPanel",position)
        {
            Color = Color.DarkSlateGray;
            originalPos = position;
            airPortPos = new Vector2(position.X, position.Y + 200);
        }
        public void SetEntity(PlanetEntity entity)
        {
            RemoveAll();

            _entity = entity;

            MyLabel labelName = new MyLabel(new Vector2(150, 30), entity.getObjectName(), GameScreen.FORCOLOR);
            labelName.TextAlign = Align.Center;
            labelName.Font = Constant.fontNormalBold;
            Add(labelName);
            int y = 320;
            int x = 50;
            int i=0;
            foreach (Task task in entity.AvailableTasks)
            {
                MyControl control = task.Button;
                control.Position = new Vector2(x, y);
                //x += 55;
                if (i == 2)
                {
                    y = 320;
                    x += 125;
                    i -= 3;
                }
                else
                {
                    y += 55;
                }
                Add(control);
                i++;
            }

            if (entity.GetType() == typeof(AirPort))
            {
                SpaceCraft spacecraft = new SpaceCraft(null);
                AirCraft airCraft = new AirCraft(null);
                BomberCraft bomberCraft = new BomberCraft(null);
                MotherShip motherShip = new MotherShip(null);
                entity.AvailableTasks[0].Button.ToolTipText = spacecraft.getUnitInfoString();
                entity.AvailableTasks[1].Button.ToolTipText = airCraft.getUnitInfoString();
                entity.AvailableTasks[2].Button.ToolTipText = bomberCraft.getUnitInfoString();
                entity.AvailableTasks[3].Button.ToolTipText = motherShip.getUnitInfoString();
                entity.AvailableTasks[3].Button.ToolTipLeft = true;
            }                               

            _labelInfo = new MyLabel(new Vector2(20, 170), entity.getInfoString(), GameScreen.FORCOLOR);
            Add(_labelInfo);

            _listLabelDetailInfo = new List<MyLabel>();
            int yy = 320;
            for (int j = 0; j < 4; j++)
            {
                MyLabel _labelDetailInfo = new MyLabel(new Vector2(120, yy), "", GameScreen.FORCOLOR);
                _labelDetailInfo.Font = Constant.fontNormalBold;
                _labelDetailInfo.TextAlign = Align.Center;
                _listLabelDetailInfo.Add(_labelDetailInfo);
                Add(_labelDetailInfo);
                yy += 55;
            }
            _listLabelDetailInfo[3].Position = new Vector2(245, 320);

            buttonDetail = new MyButton(new Vector2(50, 430), "Detail");
            buttonDetail.Visible = false;
            buttonDetail.MousePressedCallBack = PressedDetailButton;
            Add(buttonDetail);

            if (_entity.GetType() == typeof(AirPort))
                Position = originalPos;
            else
                Position = airPortPos;
        }

        public override void UpdateInfo(GameTime gameTime)
        {
            if (_entity == null || !Visible)
                return;

            _currentTotalTime += gameTime.ElapsedGameTime.Milliseconds;
            if (_currentTotalTime > 100)
            {
                _currentTotalTime = 0;
                buttonDetail.Visible = false;
                _labelInfo.Text = _entity.getInfoString();

                if (_entity.GetType() == typeof(AirPort))
                {
                    List<int> listInfo = _entity.PlanetOwner.GetNumberOfUnitPerType();
                    for (int i = 0; i < listInfo.Count; i++)
                    {
                        _listLabelDetailInfo[i].Text = listInfo[i].ToString();
                    }
                }
                else
                {
                    buttonDetail.Visible = false;
                }
            }
        }

        private void PressedDetailButton(MyControl sender)
        {
            showDetailPanelEvent();
        }
    }
}
